package utils;

public class SynchQueue {

    private Message first = null;
    private Message last = null;

    public synchronized void add(String p) {
        if (this.first == null ) { // la queue est vide
            this.first = new Message(p, first);
            this.last = this.first;
        } else {
            this.last.next = new Message(p, null); // on ajoute les paquets a la fin
            this.last = this.last.next;
        }
        notifyAll();
    }

    public synchronized String get() {
        try {
            while (first == null) {
                wait();
            }
        } catch (InterruptedException e) {
        }
        String toReturn = first.getMSG();
        first = first.getNext();
        return toReturn;
    }
}
